﻿/*
 * Created by SharpDevelop.
 * User: alex
 * Date: 02.05.2011
 * Time: 22:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;
using System.IO;

namespace DrawTest
{
	
	public partial class MainForm : Form
	{
		private const int angleIncrement = 2;
		private Image imageMain;
		private Image imageCircle;
		private bool captureCoordinates = false;
		private SecondForm secondForm;
		private DataStore dataStore = new DataStore();

		
		public MainForm()
		{
			InitializeComponent();
		}
		
		
		
		void initializeFarLines(List <FarLine> azimuthLines)
		{
			for (int i=0;i<36;i++)
			{
				FarLine farLine = new FarLine();
				if (i%3 == 0){
					farLine.setReference();//make more thick
				}
				farLine.setStableAngle(i*10);
				azimuthLines.Add(farLine);
			}
		}

		
		void drawAzimuthLines(List <FarLine> azimuthLines, Graphics dc)//called by timer
		{
			foreach (FarLine farLine in azimuthLines){
				farLine.draw(dataStore.CurrentAngle, dc);//draw each line on its position and line itself determines its transparency
			}
		}
		
		
		void initializeAirCrafts(List <AirCraft> airCrafts, int targetCount, int battleTime, bool enableFriendTgr)
		{
			airCrafts.Clear();
			
			AirCraftGenerator airCraftsGenerator = new AirCraftGenerator();
			airCraftsGenerator.generatorAirCraft(targetCount,airCrafts,battleTime,true);
			
			if (enableFriendTgr){
				airCraftsGenerator.generatorAirCraft(2,airCrafts,10,false);
			}
		}

		
		void addAttackingMissile(List<Missile> missiles, int azimuth)
		{
			Missile missile = new Missile();
			
			MyPoint startPoint = new MyPoint(Consts.CoordCenterOfRotationX,Consts.CoordCenterOfRotationY);
			MyAngleLen angleLen = new MyAngleLen(azimuth, 215);
			MyPoint endPoint = FlyingObject.FindDecartPoint(startPoint, angleLen);
			
			string picture = "resources\\fly_missile.png";
			
			missile.initMissile(picture,5,startPoint, endPoint);
			dataStore.Missiles.Add(missile);
		}

		
		void drawFlyingObjects(List <AirCraft> airCrafts, List <Missile> missiles, int currentAngle, Graphics dc)
		{
			for (int i=0; i< airCrafts.Count; i++){
				AirCraft airCraft = airCrafts[i];
				airCraft.drawAirCraft(dc, currentAngle);
				if (airCraft.IsKilled){
					airCrafts.Remove(airCraft);
					i--;
				}
			}
			for (int i=0; i< missiles.Count; i++){
				Missile missile = missiles[i];
				missile.drawMissile(dc,currentAngle,airCrafts);
				if ( missile.IsKilled){
					missiles.Remove( missile);
					i--;
				}
			}
		}
		
		
		void MainFormLoad(object sender, EventArgs e)
		{
			initializeFarLines(dataStore.AzimuthLines);
			
			dataStore.MissileLeft = Consts.MissileCount;
			imageMain = Image.FromFile("resources\\main.png");
			imageCircle = Image.FromFile("resources\\detail_cool.png");
			mainTimer.Interval = Consts.TimerInterval;
			secondForm = new SecondForm();
			secondForm.initialize(dataStore);
		}
		
		
		void BtnStartClick(object sender, EventArgs e)
		{
			int tgrCount = Convert.ToInt32(textTargetCount.Text);
			int battleTime = Convert.ToInt32(textBattleTime.Text)*60;
			initializeAirCrafts(dataStore.AirCrafts, tgrCount, battleTime, checkFriendTrg.Checked);
			
			mainTimer.Enabled = !mainTimer.Enabled;
			
			if (mainTimer.Enabled){
				btnStart.Text = "Стоп";
			} else {
				btnStart.Text = "Старт";
			}
		}
		
		
		
		void PaintFuckingThings(Graphics dc)
		{
			RectangleF rect = new RectangleF(0,0,Consts.SizeMainDetails,Consts.SizeMainDetails);
			dc.DrawImage(imageMain, rect);//draw background
			
			dc.TranslateTransform(Consts.CenterOfRotationX,Consts.CenterOfRotationY);//set zero coordinates at predefined points (center of circle in image)
			dc.RotateTransform(dataStore.CurrentAngle);
			rect = new RectangleF((-Consts.SizeDetails)/2,(-Consts.SizeDetails)/2,Consts.SizeDetails,Consts.SizeDetails);
			dc.DrawImage(imageCircle,rect);//draw partial circle lines and needed angle
			
			drawAzimuthLines(dataStore.AzimuthLines, dc);//draw azimuth lines (not far in reality)
			
			
			//Translate to initial state
			dc.RotateTransform(-dataStore.CurrentAngle);
			dc.TranslateTransform(-Consts.CoordCenterOfRotationX,-Consts.CoordCenterOfRotationY);
			
			if (captureCoordinates){//ignore this, just red square for tests
				Rectangle rectC = new Rectangle(0,0,Consts.SizeOfCoordSystem,Consts.SizeOfCoordSystem);
				Pen p = new Pen(Color.Red,1);
				dc.DrawRectangle(p,rectC);
			}
			
			drawFlyingObjects(dataStore.AirCrafts, dataStore.Missiles, dataStore.CurrentAngle, dc);
			
			TestPaintRec(dc);
			
			dc.TranslateTransform(Consts.CoordCenterOfRotationX,Consts.CoordCenterOfRotationY);
			dc.RotateTransform(dataStore.CurrentAngle);
			
		}
		
		
		
		void TestPaintRec(Graphics dc)
		{	
			int cathetHeight = 0;
			
			MyPoint centerRotation = new MyPoint(Consts.CoordCenterOfRotationX, Consts.CoordCenterOfRotationY);
			MyPoint bottomRight = new MyPoint(Consts.CoordCenterOfRotationX + Consts.WidthRecSecForm/2,Consts.CoordCenterOfRotationY);
			MyPoint bottomLeft = new MyPoint(Consts.CoordCenterOfRotationX - Consts.WidthRecSecForm/2,Consts.CoordCenterOfRotationY);
			cathetHeight = (int)Math.Round(Math.Sqrt(Consts.FarLineLenght*Consts.FarLineLenght - Consts.WidthRecSecForm*Consts.WidthRecSecForm/4));
			MyPoint topRight = new MyPoint(bottomRight.X,Consts.FarLineLenght - cathetHeight);
			MyPoint topLeft = new MyPoint(bottomLeft.X,Consts.FarLineLenght - cathetHeight);
			//находим точки прямоугольника расположенного на 0 градусов
			
			MyAngleLen pointToRotaBL = FlyingObject.FindAngleAndLength(bottomLeft, centerRotation);
			MyAngleLen pointToRotaBR = FlyingObject.FindAngleAndLength(bottomRight, centerRotation);
			MyAngleLen pointToRotaTR = FlyingObject.FindAngleAndLength(topRight, centerRotation);
			MyAngleLen pointToRotaTL = FlyingObject.FindAngleAndLength(topLeft, centerRotation);
			//переводим из декар. сис. в угол-длина
			
			pointToRotaBL.Ang+=dataStore.angleRotatRec;
			pointToRotaBR.Ang+=dataStore.angleRotatRec;
			pointToRotaTR.Ang+=dataStore.angleRotatRec;
			pointToRotaTL.Ang+=dataStore.angleRotatRec;
			//добавляем нужный угол
			
			bottomLeft = FlyingObject.FindDecartPoint(centerRotation, pointToRotaBL);
			bottomRight = FlyingObject.FindDecartPoint(centerRotation, pointToRotaBR);
			topRight = FlyingObject.FindDecartPoint(centerRotation, pointToRotaTR);
			topLeft = FlyingObject.FindDecartPoint(centerRotation, pointToRotaTL);
			//переводим обратно в декар. сис.
			
			List<MyPoint>  leftRecSide= FlyingObject.findPath(bottomLeft,topLeft);
			List<MyPoint>  rightRecSide= FlyingObject.findPath(bottomRight,topRight);
			
			int minLen = Math.Min(leftRecSide.Count,rightRecSide.Count);
			List<MyPoint> allRecPoint = new List<MyPoint>();
			List<MyPoint> clipboard = new List<MyPoint>();
			
			for(int i=0; i<minLen; i++){
				clipboard = FlyingObject.findPath(leftRecSide[i],rightRecSide[i]);
				allRecPoint.AddRange(clipboard);
			}
			
			Pen blackPen = new Pen(Color.Red, 2);
			Point point1 = new Point(bottomLeft.X, bottomLeft.Y);
			Point point2 = new Point(bottomRight.X, bottomRight.Y);
			Point point3 = new Point(topLeft.X, topLeft.Y);
			Point point4 = new Point(topRight.X, topRight.Y);
			dc.DrawLine(blackPen, point1, point2);
			dc.DrawLine(blackPen, point3, point4);
			dc.DrawLine(blackPen, point1, point3);
			dc.DrawLine(blackPen, point2, point4);
		}
		
		
		void MainFormPaint(object sender, PaintEventArgs e)
		{
			PaintFuckingThings(e.Graphics);
			
		}
		
		
		void mainTimerTick(object sender, EventArgs e)
		{
			dataStore.CurrentAngle+=angleIncrement;
			if (dataStore.CurrentAngle >= 360){
				dataStore.CurrentAngle = 0;
			}
			curAngle.Text = dataStore.CurrentAngle.ToString();
			secondForm.onTimer();
			Invalidate();
		}

		
		
		void MainFormKeyDown(object sender, KeyEventArgs e)
		{
			
			if(e.KeyCode == Keys.A){
				captureCoordinates = true;
			}
			if(e.KeyCode == Keys.S){
				captureCoordinates = false;
			}
			Invalidate();
		}

		
		void MainFormMouseMove(object sender, MouseEventArgs e)
		{
			if (captureCoordinates){
				int coordX = e.X - Consts.CenterOfRotationX + Consts.CoordCenterOfRotationX;
				int coordY = e.Y - Consts.CenterOfRotationY + Consts.CoordCenterOfRotationY;
				textCoordX.Text = coordX.ToString();
				textCoordY.Text = coordY.ToString();
			}
		}
		
		
		
		void BtnMissileClick(object sender, EventArgs e)
		{
			if (dataStore.MissileLeft > 0 ){
				addAttackingMissile(dataStore.Missiles, Convert.ToInt32(textAzimuth.Text));
				dataStore.MissileLeft--;
				lblRocketCount.Text = dataStore.MissileLeft.ToString() + "/" + Consts.MissileCount.ToString();
			}
		}
		
		
		
		void BtnShowFormClick(object sender, EventArgs e)
		{
			secondForm.showForm();
		}
		
		
		
		void TrackBar1Scroll(object sender, EventArgs e)
		{
			dataStore.angleRotatRec = trackBar1.Value;
		}
	}
}
